import Vue from 'vue';
import Vuex from 'vuex'
import {commit} from "lodash/seq";
import {channelList} from '@/api/share';
import {encryption} from '@/utils/index'
import {loginByUsername} from '@/api/login'
import {getUserInfo} from '@/api/my';
import httpRequest from "@/api/httpRequest";
import chatStore from './modules/chatStore'; //引入聊天的store,能复用就复用
import friendStore from './modules/friendStore';
import uiStore from "./modules/uiStore";
import userStore from './modules/userStore';
import my from './modules/my';
import share from './modules/share';
import shopping from "./modules/shopping";
import trends from "./modules/trends";
import {userInfo} from "@/api/api";
import {Avatar} from "element-ui";

Vue.use(Vuex)
// https://webpack.js.org/guides/dependency-management/#requirecontext
// const modulesFiles = require.context('./modules', true, /\.js$/)

// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
// const modules = modulesFiles.keys().reduce((modules, modulePath) => {
//     // set './app.js' => 'app'
//     const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
//     const value = modulesFiles(modulePath)
//     modules[moduleName] = value.default
//     return modules
// }, {})

Vue.use(Vuex)
const store = new Vuex.Store({
    // modules,
    state: {
        imgUrl: 'https://debug.yasee.com.cn',
        // imgUrl: 'https://wechat.yasee.com.cn',
        num: 1,
        userInfo: {
            avatar: ''
        },
        flag: 0,//是否创建socket链接
        isShopping: 0,
        //websocket 链接 及子系统接口请求路径
        websocket: 'wss://debug.yasee.com.cn:8878/imei',//测试环境
        // websocket:'ws://192.168.90.225:8878/imei',//本地服务
        // websocket:'wss://wechat.yasee.com.cn:8878/imei',//生产环境
        //子系统接口请求路径
        baseUrlSecond: 'https://debug.yasee.com.cn/imei',//测试环境
        // baseUrlSecond:'https://wechat.yasee.com.cn/imei',//生产环境
        failImageList: []
    },
    mutations: {
        SET_FAILIMAGELIST: (state, data) => {
            state.failImageList.push(data)
            console.log('failImageList', state.failImageList)
        },
        SET_TOKEN: (state, data) => {
            state.token = 'Bearer ' + data
        },
        SET_USERINFO: (state, data) => {
            console.log('------SET_USERINFO----', data)
            state.userInfo = data
        },
        SET_AVATAR: (state, data) => {
            state.userInfo.avatar = data
        },
        SET_CHILDREN_USER_INFO: (state, data) => {
            console.log('子系统用户信息', data)
            state.childrenUserInfo = data
        },
        SET_FlAG: (state, data) => {
            state.flag = data
        },
        initStore(state) {
            this.commit("initChatStore");
            // this.commit("initFriendStore");//通讯录
            // this.commit("initGroupStore"); //群组
        },

    },
    actions: {
        //配合申请H5支付 模拟的登录
        LoginByUsername({commit}, userInfo) {
            const user = encryption({
                data: userInfo, key: 'yassyassyassyass', param: ['password']
            })
            return new Promise((resolve, reject) => {
                loginByUsername(user.username, user.password, user.code, user.randomStr).then(response => {
                    const data = response
                    commit('SET_TOKEN', data.access_token)
                    // commit('SET_REFRESH_TOKEN', data.refresh_token)
                    // commit('CLEAR_LOCK')
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //获取主系统的用户信息
        getUserInfo({commit}, data) {
            let params = {
                id: '' //别人访问个人主页的时候 需要填写id
            }
            return new Promise((resolve, reject) => {
                getUserInfo(params).then((res) => {
                    const data = res.data
                    commit('SET_USERINFO', data)
                    resolve()
                })
                    .catch(error => {
                        reject(error)
                    })
            })
        },
        //那到主系统的用户信息-去登录子系统
        loginChat({commit}, userInfo) {
            //登录操作
            let loginForm = {
                userName: userInfo.username ? userInfo.username : '',
                password: '123456',
                nickName: userInfo.nickname ? userInfo.nickname : '',
                avatar: userInfo.avatar ? userInfo.avatar : ''
            }
            return new Promise((resolve, reject) => {
                httpRequest({
                    url: "/yaseeLogin",
                    method: 'post',
                    data: loginForm
                })
                    .then((data) => {
                        resolve(data)
                        console.log('登录子系统用户成功', data)
                        commit('SET_FlAG', 1); //是否登录子系统
                        sessionStorage.setItem("accessToken", data.accessToken);
                        sessionStorage.setItem("refreshToken", data.refreshToken);
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        updateAvatar({commit}, avatar) {
            commit('SET_AVATAR', avatar)
        },
        updateFailImageList({commit},data) {
            commit('SET_FAILIMAGELIST', data)
        }
    },
    modules: {
        chatStore,
        friendStore,
        uiStore,
        userStore,
        my,
        share,
        shopping,
        trends
    }
})

export default store
